package com.xinke.module.system.controller.admin.user;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.Result;
import com.xinke.component.common.util.object.BeanUtils;
import static com.xinke.component.common.pojo.Result.success;

import com.xinke.component.excel.excel.core.util.ExcelUtils;

import com.xinke.component.web.apilog.core.annotation.ApiAccessLog;
import static com.xinke.component.web.apilog.core.enums.OperateTypeEnum.*;

import com.xinke.module.system.model.vo.user.*;
import com.xinke.module.system.model.entity.user.UserRoleDO;
import com.xinke.module.system.service.user.UserRoleService;

@Tag(name = "管理后台 - 用户和角色关联")
@RestController
@RequestMapping("/system/user-role")
@Validated
public class UserRoleController {

    @Resource
    private UserRoleService userRoleService;

    @PostMapping("/create")
    @Operation(summary = "创建用户和角色关联")
    @PreAuthorize("@ss.hasPermission('system:user-role:create')")
    public Result<Long> createUserRole(@Valid @RequestBody UserRoleSaveReqVO createReqVO) {
        return success(userRoleService.createUserRole(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户和角色关联")
    @PreAuthorize("@ss.hasPermission('system:user-role:update')")
    public Result<Boolean> updateUserRole(@Valid @RequestBody UserRoleSaveReqVO updateReqVO) {
        userRoleService.updateUserRole(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户和角色关联")
    @Parameter(name = "userRoleId", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:user-role:delete')")
    public Result<Boolean> deleteUserRole(@RequestParam("userRoleId") Long userRoleId) {
        userRoleService.deleteUserRole(userRoleId);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户和角色关联")
    @Parameter(name = "userRoleId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:user-role:query')")
    public Result<UserRoleRespVO> getUserRole(@RequestParam("userRoleId") Long userRoleId) {
        UserRoleDO userRole = userRoleService.getUserRole(userRoleId);
        return success(BeanUtils.toBean(userRole, UserRoleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户和角色关联分页")
    @PreAuthorize("@ss.hasPermission('system:user-role:query')")
    public Result<PageResult<UserRoleRespVO>> getUserRolePage(@Valid UserRolePageReqVO pageReqVO) {
        PageResult<UserRoleDO> pageResult = userRoleService.getUserRolePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserRoleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户和角色关联 Excel")
    @PreAuthorize("@ss.hasPermission('system:user-role:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserRoleExcel(@Valid UserRolePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserRoleDO> list = userRoleService.getUserRolePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户和角色关联.xls", "数据", UserRoleRespVO.class,
                        BeanUtils.toBean(list, UserRoleRespVO.class));
    }

}